

#include <QApplication>
#include <QObject>
#include<QDebug>
#include<QFile>
#include<QTextStream>
#include <QProcess>
#include <QFile>
#include <QFileInfo>
#include<QResource>
#include<QMessageBox>
#include<QThread>
#include"loginwidget.h"
#include"changeidwidget.h"
#include "api/tcpserver.h"

#define MYLOG qDebug() << "[" << __FILE__ << ":" << __LINE__ << ":" << QThread::currentThread() << __func__ << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")<< "]"
void outputMessage(QtMsgType type,const QMessageLogContext &context,const QString &msg)
{
  static QMutex mutex;
  mutex.lock();
  QByteArray localMsg = msg.toLocal8Bit();
  QString text;
  switch (type)
    {
    case QtDebugMsg:
      text = QString("Debug:");
      break;
    case QtWarningMsg:
      text = QString("Warning:");
      break;
    case QtCriticalMsg:
      text = QString("Critical:");
      break;
    case QtFatalMsg:
      text = QString("Fatal:");
      break;
    default:
      text = QString("Debug:");
    }

  // 设置输出信息格式
  QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line); // F文件L行数
  QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
  //QString strMessage = QString("%1 %2 \t%3 \t%4").arg(text).arg(context_info).arg(strDateTime).arg(msg);
  QString strMessage = QString("%1 \t%2").arg(text).arg(msg);
  // 输出信息至文件中（读写、追加形式）
  QFile file(QString("log/"+QDateTime::currentDateTime().toString("yyyy-MM-dd").append("-log.txt")));
  file.open(QIODevice::ReadWrite | QIODevice::Append);
  QTextStream stream(&file);
  stream << strMessage << "\r\n";
  file.flush();
  file.close();
  // 解锁
  mutex.unlock();
}



int main(int argc, char *argv[])
{
    qInstallMessageHandler(outputMessage);//安装消息处理程序
    qputenv("QT_AUTO_SCREEN_SCALE_FACTOR","1");
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);

    QApplication a(argc, argv);
    QResource::registerResource("./automatic.rcc");

    QThread threadSever;
    TcpServer sever;
    sever.moveToThread(&threadSever);
    QObject::connect(&threadSever,&QThread::started,&sever,&TcpServer::start);
    threadSever.start();

//        LoginWidget w;
//        w.show();
    MainWindow m;
    m.show();

    return a.exec();
}
